import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from commom.utils import get_driver, quit_driver


class TestOrder:

    # 类前置方法
    def setup_class(self):
        # 获取浏览器驱动对象
        self.driver = get_driver()
        # 打开测试页
        self.driver.get("http://192.168.121.134:8082")

    def setup_method(self):
        # 等待3秒
        time.sleep(3)

    def teardown_class(self):
        # 关闭浏览器驱动对象
        quit_driver(self.driver)

    # 登录
    def test01_login(self):
        # class属性
        # elements = self.driver.find_elements(By.CLASS_NAME,"van-tabbar-item__text")
        # elements[3].click()
        # #xpach定位
        # self.driver.find_element(By.XPATH,"//*[text()='我的']").click()
        self.driver.find_element(By.XPATH, "//*[contains(text(),'我的')]").click()
        # 登录
        self.driver.find_element(By.CSS_SELECTOR, 'input[name="user"]').send_keys("user123")  # 输入账号
        self.driver.find_element(By.CSS_SELECTOR, 'input[name="password"]').send_keys("user123")  # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, ".van-button.van-button--danger.van-button--large").click()  # 点击登录按钮

    # 搜索
    def test02_search(self):
        # 1.点击【精选】回到首页
        self.driver.find_element(By.XPATH, "//*[contains(text(),'精选')]").click()
        # 2.点击搜索按钮
        self.driver.find_element(By.CLASS_NAME, 'van-field__control').click()
        time.sleep(1)
        # 3.输入关键字并回撤
        element = self.driver.find_element(By.CLASS_NAME, 'van-field__control')
        element.send_keys("母亲节")
        time.sleep(1)
        element.send_keys(Keys.ENTER)
        pass

    # 加入购物车
    def test03_add_cart(self):
        # 1.点击图片
        self.driver.find_element(By.CLASS_NAME, 'van-image__img').click()
        # 2.点击商品规格
        self.driver.find_element(By.CSS_SELECTOR,
                                 '.van-button.van-button--warning.van-button--large.van-button--square.van-goods-action-button.van-goods-action-button--first.van-goods-action-button--warning').click()
        elements = self.driver.find_elements(By.CLASS_NAME, "van-sku-row__item-name")
        elements[0].click()
        time.sleep(2)
        elements[2].click()
        # 3. 点击加入购物车
        self.driver.find_element(By.CSS_SELECTOR, '.van-button.van-button--warning.van-button--large').click()
        # 再次点击购物车
        self.driver.find_elements(By.CLASS_NAME, 'van-goods-action-icon')[0].click()
        pass

        # 提交订单

    def test04_submit_order(self):
        # 1、点击结算
        # self.driver.find_element(By.CSS_SELECTOR,
        #                          "#app > div.tab-cart.view-router > div.van-submit-bar > div > button").click()
        # time.sleep(1)
        # 设置显式等待
        WebDriverWait(self.driver, 10, 0.5).until(lambda x: x.find_element(By.CSS_SELECTOR,
                                                                           "#app > div.tab-cart.view-router > div.van-submit-bar > div > button")).click()
        # 2、填写收货地址（前置：手工测试已维护地址信息）
        # 3、提交订单
        # self.driver.find_element(By.CSS_SELECTOR, "#app > div > div.van-submit-bar > div > button").click()
        # time.sleep(1)
        WebDriverWait(self.driver, 10, 0.5).until(lambda x: x.find_element(By.CSS_SELECTOR,
                                                                               "#app > div > div.van-submit-bar > div > button")).click()

        # 支付

    def test05_pay(self):
        print("支付")
